import requestPost from '@/utils/request.js';  
  var amapFile = require('@/utils/amap-wx.130.js');
/**
 * 复制属性方法
 * @param target
 * @param source
 * @returns {object}
 */
export function copyProperties(target, source) {
	if (!target || !source) {
		return target;
	}
	for (let e in target) {
		target[e] = e in source ? source[e] : target[e];
	}
	return target;
}

/**
 * 生成16位的uuid
 */
export function getUUID() {
	return 'xxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = (Math.random() * 16) | 0,
			v = c == 'x' ? r : (r & 0x3) | 0x8;
		return v.toString(16);
	});
}

export function queryStringify(data){
  let result = [];
  for (let key in data) {
    let value = data[key];
    if (Array.isArray(value)) {
      value.forEach(item => {
        result.push(key + "=" + item);
      })
    } else {
      result.push(key + '=' + value);
    }
  }
  return `?${result.join('&')}`;
}

export function throttle(fn, interval) {
  var enterTime = 0;//触发的时间
  var gapTime = interval || 300;//间隔时间，如果interval不传，则默认300ms
  return function () {
    var context = this;
    var backTime = new Date();//第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context, arguments);
      enterTime = backTime;//赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}
 
export function currentPosition(){
  return new Promise((resolve, reject) => {
	wx.getFuzzyLocation({
	 type: 'gcj02',
	 success (res) {
	   resolve({
	     latitude: res.latitude,
	     longitude: res.longitude
	   })
	 },
	 fail(error){
		console.log('getFuzzyLocation===========fail',error)
		reject({msg: 'fail'}) 
	 }
	})
  })
}
export function distance(lat1,lng1,lat2,lng2){
  return new Promise((resolve, reject) => {
    requestPost("/position/getDistance",{lat1,lng1,lat2,lng2},"/baseplatform").then(res => {
      resolve(res.data || 0)
    }).catch(error => {
      reject(error)
    })
  })
}